﻿using CS.BLL;
using CS.model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace chaoshiguanli
{
    public partial class Admin : Form
    {
        public Admin()
        {
            InitializeComponent();
        }

        public GetAdmin User = new GetAdmin();
        public List<Admininfo> ad = new List<Admininfo>();
        private void button1_Click(object sender, EventArgs e)
        {
            if (txtUser.Text == "" && txtPwd.Text == "")
            {
                MessageBox.Show("请输入要添加的数据");
            }
            else
            {
                if (User.InsertUser(txtUser.Text, txtPwd.Text))
                {
                    MessageBox.Show("添加数据成功！");
                    btnSelect_Click(null, null);
                    txtUser.Text = txtPwd.Text = "";
                }
                else
                {
                    MessageBox.Show("添加数据失败！");
                }
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (ad.Count>0) 
            {
                ad.Clear();
            }
            BANG();
        }

        private void BANG()
        {
            List<Admininfo> g = User.AdminGet(txtName.Text);
            if (txtName.Text != "")
            {
                Admininfo item = new Admininfo();
                if (g.Count == 0)
                {
                    MessageBox.Show("为查询到数据");
                }
                else if (g.Count == 1)
                {
                    item.UserName = g[0].UserName;
                    item.UserPass = g[0].UserPass;
                    ad.Add(item);
                }
                shuax();
                txtName.Text = "";
            }
            else
            {
                for (int i = 0; i < g.Count; i++)
                {
                    Admininfo item = new Admininfo();
                    item.UserName = g[i].UserName;
                    item.UserPass = g[i].UserPass;
                    ad.Add(item);
                }
                shuax();
            }
        }
        private void shuax()
        {
            dgvSelect.DataSource = new BindingList<Admininfo>(ad);
        }
        private void Admin_Load(object sender, EventArgs e)
        {
        }

    }
}
